package com.happyma.bbn;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;

import com.happyma.bbn.utils.ContextGeeter;

import java.util.Locale;

public class SettingsFragment extends PreferenceFragmentCompat implements Preference.OnPreferenceClickListener {

    public static final int LANGUAGE_CHANGED = 1001;
    public static final String LANGUAGE_SETTING="lang_setting";
    private static final String TAG = "SettingFragment->";

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        addPreferencesFromResource(R.xml.settings);
        findPreference(getString(R.string.pref_key_language_en)).setOnPreferenceClickListener(this);
        findPreference(getString(R.string.pref_key_language_es)).setOnPreferenceClickListener(this);
        findPreference(getString(R.string.pref_key_language_pt)).setOnPreferenceClickListener(this);
        findPreference(getString(R.string.pref_key_language_de)).setOnPreferenceClickListener(this);
        findPreference(getString(R.string.pref_key_language_ru)).setOnPreferenceClickListener(this);
        findPreference(getString(R.string.pref_key_language_zh_cn)).setOnPreferenceClickListener(this);
        findPreference(getString(R.string.pref_key_language_zh_tw)).setOnPreferenceClickListener(this);
        findPreference(getString(R.string.pref_key_language_ja)).setOnPreferenceClickListener(this);
        findPreference(getString(R.string.pref_key_language_ko)).setOnPreferenceClickListener(this);
    }

    @Override
    public boolean onPreferenceClick(Preference preference) {
            String key;
        if (preference != null) {
            key = preference.getKey();
        }else {
            key = "";
        }
        if (key.equals(getString(R.string.pref_key_language_en))){
            return setLanguage("en_us", "en", "us");
        }
        if (key.equals(getString(R.string.pref_key_language_es))){
            return setLanguage("es", "es", "");
        }
        if (key.equals(getString(R.string.pref_key_language_pt))){
            return setLanguage("pt_br", "pt", "br");
        }
        if (key.equals(getString(R.string.pref_key_language_de))){
            return setLanguage("de", "de", "");
        }
        if (key.equals(getString(R.string.pref_key_language_ru))){
            return setLanguage("ru", "ru", "");
        }
        if (key.equals(getString(R.string.pref_key_language_zh_cn))){
            return setLanguage("zh_cn", "zh", "cn");
        }
        if (key.equals(getString(R.string.pref_key_language_zh_tw))){
            return setLanguage("zh_tw", "zh", "tw");
        }
        if (key.equals(getString(R.string.pref_key_language_ja))){
            return setLanguage("ja", "ja", "");
        }
        if (key.equals(getString(R.string.pref_key_language_ko))){
            return setLanguage("ko", "ko", "");
        }

        return false;
    }

    public boolean setLanguage(String keyValue, String language, String country){
        SharedPreferences defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
        Resources resources = getResources();
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        Configuration configuration = resources.getConfiguration();
        configuration.locale = new Locale(language, country);
        resources.updateConfiguration(configuration, displayMetrics);
        defaultSharedPreferences.edit().putString(LANGUAGE_SETTING, keyValue).apply();
        getActivity().setResult(LANGUAGE_CHANGED);
        getActivity().finish();
        return true;
    }

    @Override
    public void onResume() {
        super.onResume();
        getActivity().setTitle(R.string.language_selection);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Intent intent = new Intent();
        intent.setAction("org.bbnradio.return.to.main.activity");
        ContextGeeter.getAppContext().sendBroadcast(intent);
    }
}